﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

///<summary>
///
///</summary>
public class StartView : MonoBehaviour
{
    Animation startAni;
    GameObject preson;
    public Sprite sprite;
	//音效
	public Sprite[] Sound;
	//音乐
	public Sprite[] Music;

	//音效委托
	public UnityAction soundAction;
	//音乐委托
	public UnityAction musicAction;

    //关闭委托
    public UnityAction Close;
    //开启委托
    public UnityAction Open;
    //开始游戏委托
    public UnityAction PlayGame;

    //设置按钮
    GameObject btnSet;
    //设置面板
    GameObject setPanel;
    //设置面板父类
    GameObject panelParent;
    //面板中的关闭按钮
    GameObject btnClose;
	//音效开关
	GameObject btnSound;
	//音乐开关
	GameObject btnMusic;

    //开始游戏按钮
    GameObject btnStart;

    // Start is called before the first frame update
    public void Init()
    {
        startAni = gameObject.GetComponent<Animation>();
        preson = GameObject.Find("Canvas/View/bgContent/playerBg");
        btnSet = GameObject.Find("Canvas/PanelManager/MainSet/Set");
        panelParent = GameObject.Find("Canvas/PanelManager");
        setPanel = panelParent.transform.Find("SettingPanel").gameObject;
        btnClose = setPanel.transform.Find("BtnClose").gameObject;
        btnStart = GameObject.Find("Canvas/View/StartButton");
		btnMusic = setPanel.transform.Find("BtnMusic").gameObject;
		btnSound = setPanel.transform.Find("BtnSound").gameObject;
		
    } 
	public void OpenOrCloseSound()
	{
		if (GameConfig.OpenOrCloseSound)
		{
			GameConfig.OpenOrCloseSound = false;
			AudioManager.GetInstance.SoundStop();
			PlayerPrefs.SetInt("OpenSound", 0);
			btnSound.GetComponent<Image>().sprite = Sound[1];
		}
		else
		{
			GameConfig.OpenOrCloseSound = true;
			PlayerPrefs.SetInt("OpenSound", 1);
			btnSound.GetComponent<Image>().sprite = Sound[0];
		}

	}
	public void OpenOrCloseMusic()
	{
		if (GameConfig.OpenOrCloseBGM)
		{
			GameConfig.OpenOrCloseBGM = false;
			AudioManager.GetInstance.MusicStop();
			PlayerPrefs.SetInt("OpenBGM", 0);
			btnMusic.GetComponent<Image>().sprite = Music[1];
		}
		else
		{
			GameConfig.OpenOrCloseBGM = true;
			AudioManager.GetInstance.PlayBGMusic();
			PlayerPrefs.SetInt("OpenBGM", 1);
			btnMusic.GetComponent<Image>().sprite = Music[0];
		}
	}

    public void OpenPanel()
    {
		AudioManager.GetInstance.Play("Sounds/Panel/click_start");
		setPanel.SetActive(true);
    }
    public void ClosePanel()
    {
		AudioManager.GetInstance.Play("Sounds/Panel/click_start");
		setPanel.SetActive(false);
    }
    public void ChangeSprite()
    {
        preson.GetComponent<SpriteRenderer>().sprite = sprite;
    }  
    private void Start()
    {
        btnClose.GetComponent<Button>().onClick.AddListener(() =>
        {
            Close();
        });
        btnSet.GetComponent<Button>().onClick.AddListener(() =>
        {
            Open();
        });
        btnStart.GetComponent<Button>().onClick.AddListener(() =>
        {
            PlayGame();
        });
		btnMusic.GetComponent<Button>().onClick.AddListener(() =>
		{
			musicAction();
		});
		btnSound.GetComponent<Button>().onClick.AddListener(() =>
		{
			soundAction();
		});
	}
	public void EnterStart()
	{
		SoundAndBGMIconShow();
		AudioManager.GetInstance.Play("Sounds/Start/magic_chime_title");
		AudioManager.GetInstance.Play("Sounds/Start/title_intro");
		AudioManager.GetInstance.PlayBGMusic();
	}

	public void SoundAndBGMIconShow()
	{
		int bgmNum = PlayerPrefs.GetInt("OpenBGM");
		if (bgmNum == 1)
			btnMusic.GetComponent<Image>().sprite = Music[0];
		else
			btnMusic.GetComponent<Image>().sprite = Music[1];
		int soundNum = PlayerPrefs.GetInt("OpenSound");
		if (soundNum == 1)
			btnSound.GetComponent<Image>().sprite = Sound[0];
		else
			btnSound.GetComponent<Image>().sprite = Sound[1];
	}
}
